<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../static/bootstrap-5.3.0-dist/css/bootstrap.min.css">
<script src="../static/bootstrap-5.3.0-dist/js/bootstrap.bundle.min.js"></script>
<link href="../static/layui-v2.9.1/layui/css/layui.css" rel="stylesheet">
<script src="../static/layui-v2.9.1/layui/layui.js"></script>
<link rel="stylesheet" href="../static/framework/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../static/framework/reset.css">
<link rel="stylesheet" href="../static/css/footer.css">
<link rel="stylesheet" href="../static/css/editRecipient.css">

<title>表单</title>

<head>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f0f0f0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 120vh;
      margin: 0;
    }


    input[type="text"],
    input[type="password"],
    select {
      width: 100%;
      padding: 10px;
      border: none;
      border-radius: 5px;
      margin-bottom: 10px;
      background-color: hsl(0, 100%, 100%);
      font-family: Arial, sans-serif;
      font-size: 14px;
    }

    input[type="radio"],
    input[type="checkbox"] {
      margin-right: 5px;
    }



    /* 示例动画效果 */
    input[type="text"]:focus,
    input[type="password"]:focus,
    select:focus {
      outline: none;
      box-shadow: 0px 0px 10px #ffffff;
      transform: scale(1.0);
      /*对元素进行缩放变换*/
      transition: all 0.2s ease;
    }

    .btn {
      display: block;
      /* 将按钮转换为块级元素 */
      margin: 0 auto;
      /* 使用auto来水平居中 */
      width: 90vw;
    }

    .footer {
      position: fixed;
      left: 0;
      bottom: 0;
      width: 100%;
      text-align: center;
      background-color: #f1f1f1;
      box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
    }



    .container {
      background-color: #fff;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      text-align: center;
      width: 300px;
    }

    h1 {
      color: #333;
      margin-bottom: 20px;
    }

    input[type="file"] {
      margin-bottom: 10px;
      display: none; /* Hide default file input */
    }

    label {
      background-color: #4CAF50;
      color: white;
      padding: 10px 20px;
      border-radius: 4px;
      cursor: pointer;
      transition: background-color 0.3s;
      display: inline-block;
    }

    label:hover {
      background-color: #45a049;
    }

    img {
      max-width: 50%;
      max-height: 50%;
      border-radius: 8px;
      margin-top: 10px;
      display: none; /* Initially hide the image */
      margin-left: auto;
      margin-right: auto;
    }

    button {
      background-color: #008CBA;
      color: white;
      border: none;
      padding: 10px 20px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
      margin-top: 10px;
      border-radius: 4px;
      cursor: pointer;
      transition: background-color 0.3s;
    }

    button:hover {
      background-color: #005f6b;
    }

    #uploadStatus {
      margin-top: 10px;
    }
  </style>

</head>

<body>
  <div class="wrapper" style="padding: 20px;">
    <header>
      <i class="fa fa-angle-left fa-2x" onclick="location.href='/myInformation'" style="font-size: 20px">首页</i>
      <p>个人信息</p>
      <p></p>
    </header>

    <br>
    <br>

    <div class="container">
      <h1>上传头像</h1>

      <!-- Label for custom file input -->
      <label for="avatarInput">选择文件</label>
      <input type="file" id="avatarInput" name="avatar" accept="image/*" onchange="previewAvatar(event)">

      <!-- Image preview area -->
      <img id="avatarPreview" src="#" alt="预览区域">

      <!-- Upload button -->
      <button onclick="uploadAvatar()">上传</button>

      <!-- Upload status message -->
      <div id="uploadStatus"></div>
    </div>
    <br>

    <form action="/user/updateinformation" method="post">

      <input type="hidden" name="phone" th:value="${session.user.phone}"/>

      姓名：<input type="text" name="realName" th:value="${session.user.realName}"/><br />

      性别：<input type="radio" name="sex" value="2">男<input type="radio" name="sex" value="1">女<br />

      出生日期：<div class="layui-form">
      <div class="layui-inline">

          <div class="layui-inline" id="ID-laydate-range">

            <div class="layui-input-inline">
              <input type="text" name="birthday" autocomplete="off" id="ID-laydate-start-date" class="layui-input" th:value="${session.user.birthday}">
            </div>
          </div>

      </div>
    </div>
      邮箱：<input type="text" name="mail" th:value="${session.user.mail}"/><br />

      身份证号：<input type="text" name="cardId" th:value="${session.user.cardId}"/><br />

      联系地址：<input type="text" name="address" th:value="${session.user.address}" /><br />

      户号：<input type="text" name="houseNumber" th:value="${session.user.houseNumber}" /><br />

      <button type="submit" class="btn btn-primary">提交</button>
    </form>

<!--    <div class="layui-form">-->
<!--      <div class="layui-form-item layui-inline">-->
<!--        <div class="layui-inline">-->
<!--          <label class="layui-form-label list-unstyled = width : 100dx">起始时间</label>-->
<!--          <div class="layui-input-inline">-->
<!--            <input type="text" name="" class="layui-input" id="ID-laydate-demo" placeholder="yyyy-MM-dd">-->
<!--          </div>-->
<!--        </div>-->
<!--      </div>-->
<!--    </div>-->

<!--    <script>-->
<!--      layui.use(function(){-->
<!--        var laydate = layui.laydate;-->
<!--        // 渲染-->
<!--        laydate.render({-->
<!--          elem: '#ID-laydate-demo'-->
<!--        });-->
<!--        // 英文版-->
<!--        laydate.render({-->
<!--          elem: '#ID-laydate-demo-en',-->
<!--          lang: 'en'-->
<!--        });-->
<!--      });-->
<!--    </script>-->

    <footer class="footer">
      <div>
        <button type="button" class="layui-btn">
          <i class="layui-icon layui-icon-left"></i>
        </button>
        <button type="button" class="layui-btn">
          <i class="layui-icon layui-icon-right"></i>
        </button>
      </div>
    </footer>
  </div>

  <script>
    const back = document.querySelector('button:first-child')
    const forword = back.nextElementSibling
    back.addEventListener('click', function () {
      //history.back()
      history.go(-1)
    })

    forword.addEventListener('click', function () {
      //history.forward()
      history.go(1)
    })
    //头像
    function uploadAvatar() {
      const input = document.getElementById('avatarInput');
      const file = input.files[0];

      if (file) {
        const formData = new FormData();
        formData.append('avatar', file);

        fetch('/upload/files', {
          method: 'POST',
          body: formData
        })
                .then(response => {
                  if (!response.ok) {
                    throw new Error('网络错误或上传失败');
                  }
                  return response.text();
                })
                .then(data => {
                  const status = document.getElementById('uploadStatus');
                  status.innerHTML = '上传成功！';
                  // Optionally, you can reset the input and preview
                  input.value = null;
                  document.getElementById('avatarPreview').style.display = 'none';
                  console.log('服务器响应:', data);
                })
                .catch(error => {
                  console.error('上传头像失败:', error.message);
                  const status = document.getElementById('uploadStatus');
                  status.innerHTML = '上传失败，请重试.';
                });
      } else {
        alert('请先选择要上传的头像！');
      }
    }



    //预览
    function previewAvatar(event) {
      const preview = document.getElementById('avatarPreview');
      preview.src = URL.createObjectURL(event.target.files[0]);
      preview.style.display = 'block'; // Display the image
    }
    //日期
    layui.use(function(){
      var laydate = layui.laydate;

      // 日期范围选择器
      layui.use('laydate', function(){
        var laydate = layui.laydate;

        // 渲染日期范围选择器
        laydate.render({
          elem: '#ID-laydate-start-date', // 指定元素
          range: false, // 单个日期选择器
          trigger: 'click' // 显示方式，这里使用'click'触发
        });
      });
    });
  </script>
</body>

</html>